package com.oplus.views.dataBaseEdit;

import java.io.IOException;
import java.io.InputStream;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorSite;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.part.EditorPart;

import com.oplus.Provider.tree.TreeLabelProvider;
import com.oplus.config.ConfigBuilder;


/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class DatabaseEditor extends EditorPart {
	private Label dbUrlLB;
	private Button cancelButton;
	private Button saveButton;
	private Text password;
	private Label passwordLB;
	private Text user;
	private Label userLB;
	private Text url;

	public DatabaseEditor() {
		// TODO Auto-generated constructor stub
	}

	public void doSave(IProgressMonitor monitor) {
		// TODO Auto-generated method stub

	}

	public void doSaveAs() {
		// TODO Auto-generated method stub

	}

	public void init(IEditorSite site, IEditorInput input)
			throws PartInitException {
		// TODO Auto-generated method stub
		this.setSite(site);
		this.setInput(input);
		this.setPartName("数据库设置");
		InputStream is = TreeLabelProvider.class.getResourceAsStream("/icons/tree/Database.ico");
		Image image = new Image(null,is);
		this.setTitleImage(image);
		

	}

	public boolean isDirty() {
		// TODO Auto-generated method stub
		return false;
	}

	public boolean isSaveAsAllowed() {
		// TODO Auto-generated method stub
		return false;
	}

	public void createPartControl(Composite parent) {
//		Config config = ConfigBuilder.getConfig("database.properties");
		FormLayout parentLayout = new FormLayout();
		parent.setLayout(parentLayout);
		parent.setSize(527, 400);
		{
			cancelButton = new Button(parent, SWT.PUSH | SWT.CENTER);
			FormData cancelButtonLData = new FormData();
			cancelButtonLData.left =  new FormAttachment(0, 1000, 225);
			cancelButtonLData.top =  new FormAttachment(0, 1000, 247);
			cancelButtonLData.width = 60;
			cancelButtonLData.height = 25;
			cancelButton.setLayoutData(cancelButtonLData);
			cancelButton.setText("取消");
			cancelButton.setSelection(true);
			cancelButton.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent evt) {
					cancelButtonWidgetSelected(evt);
				}
			});
		}
		{
			saveButton = new Button(parent, SWT.PUSH | SWT.CENTER);
			FormData saveButtonLData = new FormData();
			saveButtonLData.left =  new FormAttachment(0, 1000, 118);
			saveButtonLData.top =  new FormAttachment(0, 1000, 247);
			saveButtonLData.width = 60;
			saveButtonLData.height = 25;
			saveButton.setLayoutData(saveButtonLData);
			saveButton.setText("保存");
			saveButton.addSelectionListener(new SelectionAdapter() {
				public void widgetSelected(SelectionEvent evt) {
					saveButtonWidgetSelected(evt);
				}
			});
		}
		{
			FormData passwordLData = new FormData();
			passwordLData.left =  new FormAttachment(0, 1000, 170);
			passwordLData.top =  new FormAttachment(0, 1000, 155);
			passwordLData.width = 160;
			passwordLData.height = 25;
			password = new Text(parent, SWT.NONE);
			password.setText(ConfigBuilder.getPassword());
			password.setLayoutData(passwordLData);
		}
		{
			passwordLB = new Label(parent, SWT.NONE);
			FormData passwordLBLData = new FormData();
			passwordLBLData.left =  new FormAttachment(0, 1000, 90);
			passwordLBLData.top =  new FormAttachment(0, 1000, 160);
			passwordLBLData.width = 65;
			passwordLBLData.height = 20;
			passwordLB.setLayoutData(passwordLBLData);
			passwordLB.setText("密码：");
		}
		{
			FormData userLData = new FormData();
			userLData.left =  new FormAttachment(0, 1000, 170);
			userLData.top =  new FormAttachment(0, 1000, 95);
			userLData.width = 160;
			userLData.height = 25;
			user = new Text(parent, SWT.NONE);
			user.setText(ConfigBuilder.getUserKey());
			user.setLayoutData(userLData);
		}
		{
			userLB = new Label(parent, SWT.NONE);
			FormData userLBLData = new FormData();
			userLBLData.left =  new FormAttachment(0, 1000, 90);
			userLBLData.top =  new FormAttachment(0, 1000, 100);
			userLBLData.width = 65;
			userLBLData.height = 20;
			userLB.setLayoutData(userLBLData);
			userLB.setText("用户名：");
		}
		{
			FormData urlLData = new FormData();
			urlLData.left =  new FormAttachment(0, 1000, 170);
			urlLData.top =  new FormAttachment(0, 1000, 35);
			urlLData.width = 350;
			urlLData.height = 20;
			url = new Text(parent, SWT.NONE);
			url.setText(ConfigBuilder.getURLKey());
			url.setLayoutData(urlLData);
		}
		{
			dbUrlLB = new Label(parent, SWT.NONE);
			FormData dbUrlLBLData = new FormData();
			dbUrlLBLData.left =  new FormAttachment(0, 1000, 90);
			dbUrlLBLData.top =  new FormAttachment(0, 1000, 40);
			dbUrlLBLData.width = 65;
			dbUrlLBLData.height = 20;
			dbUrlLB.setLayoutData(dbUrlLBLData);
			dbUrlLB.setText("URL:");
		}

		// TODO Auto-generated method stub
//		Label title = new Label(parent, SWT.NONE); 
//	    title.setAlignment(SWT.CENTER); 
//	    
//	    title.setBackground(parent.getDisplay().getSystemColor(SWT.COLOR_BLACK)); 
//	    title.setForeground(parent.getDisplay().getSystemColor(SWT.COLOR_WHITE)); 
//	    title.setFont(new Font(parent.getDisplay(), "arial black", 16, SWT.NORMAL)); 
//	    title.setText("Login"); 
//	    title.setBounds(80,15,140,30);
//	    
//	    Label lName = new Label(parent, SWT.NONE); 
//	    lName.setText("User Name:"); 
////	    lName.setBackground(YELLOW); 
//	    lName.setBounds(30,55,80,20);
	    
//		Composite comp = new Composite(parent, SWT.NONE);
//		comp.setLayout(new FillLayout());
//		Text text = new Text(comp,SWT.LEFT);
//		  new Button(comp, SWT.PUSH).setText("Button 3");
	}

	public void setFocus() {
		// TODO Auto-generated method stub

	}
	
	private void saveButtonWidgetSelected(SelectionEvent evt) {
		String urlStr = url.getText();
		String userStr = user.getText();
		String passwrd = password.getText();
		ConfigBuilder.setPassword(passwrd);
		ConfigBuilder.setURLKey(urlStr);
		ConfigBuilder.setUserKey(userStr);
		try {
			ConfigBuilder.toStoreFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
		System.out.println("saveButton.widgetSelected, event="+evt);
		//TODO add your code for saveButton.widgetSelected
	}
	
	private void cancelButtonWidgetSelected(SelectionEvent evt) {
		System.out.println("cancelButton.widgetSelected, event="+evt);
//		IEditorInput edit = new DatabaseEditorInput();
//		IWorkbenchPage workbenchPage = this.getSite().getPage();
//		IEditorPart editor = workbenchPage.findEditor(edit);
//		if(editor != null){
//			workbenchPage.closeEditor(editor,true);
//		
//		}
		//TODO add your code for cancelButton.widgetSelected
	}

}
